< previous page page_92 next page >

Page 92
Form Events
A form has many events you can respond to. So far, youve just built one form, and youve pressed the Run button (or F5) to start your program. Visual Basic takes care of the details of displaying this form. Later, youll write code to display a form, such as this:
9f31265bb40b592b1929e18a47ab28e5.gif
frmEmployee.Show
Show is the method that each form has attached to it. A method is a bit of internal code for a form or control that performs some action on that object. When the Show method is invoked on this form, youre telling the form it should load and display itself. A form knows how to display itself based on the properties youve set in the Property window at design time.
Form_Initialize( )
The Form_Initialize() event is the first event to occur when you invoke the Show method on a form. In this event, no controls are yet loaded into memory, so you can only affect any memory variables. Referencing any controls in this event will cause a runtime error to occur. This event will only occur one time.
Form_Load( )
The second event that occurs after the Show method is invoked is Form_Load(). In this event, the form is loaded into memory. The form isnt displayed yet, but the form and its controls are now loaded. This event will only occur one time.
To place code in the Form_Load() event, double-click anywhere on the blank form and add some code. For now, just type in the following code in the code window:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub Form_Load ()
   MsgBox "This is the Form_Load() Event"
End Sub
Now just run the form to see a message box appear on the screen. Notice how the message box appears, but the form has not yet been displayed. Also notice that your program has now been frozen until you respond by clicking the OK button on this message box. Once you click this button, your form is displayed.
Common uses of the Form_Load() event are to set initial variable values, reposition the form to a certain location, load data from a table into a combo box, or place some default data into certain text boxes. For now, because you havent

 
< previous page page_92 next page >

If you like this book, buy it!